<template>
  <div class="login-wrapper">
    <div class="info">
      <div class="logo"></div>
      <div class="text"></div>
    </div>
    <div class="modal">
      <el-form :model="user" status-icon :rules="rules" ref="userForm">
        <div class="title">登陆</div>
        <el-form-item prop="userName">
          <el-input
            type="text"
            prefix-icon="el-icon-user"
            v-model="user.userName"
          />
        </el-form-item>
        <el-form-item prop="userPwd">
          <el-input
            type="password"
            prefix-icon="el-icon-view"
            v-model="user.userPwd"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn-login" @click="login"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import storage from '@/utils/storage'
import { generateRoute } from '@/utils/date'
const modules = import.meta.glob('../views/*.vue')

export default {
  name: 'Login',
  data() {
    return {
      user: {
        userName: 'admin' || this.$store.state.userInfo.userName,
        userPwd: 'admin'
      },
      rules: {
        userName: [
          { required: true, message: '请输入用户名', trigger: "blur" }
        ],
        userPwd: [
          { required: true, message: "请输入密码", trigger: "blur" }
        ]
      }
    }
  },
  methods: {
    login() {
      this.$refs.userForm.validate(async (valid) => {
        if (valid) {
          let res = await this.$api.login(this.user)
          this.$store.commit('saveUserInfo', res)
          await this.loadAsyncRoutes()
          this.$router.push('/welcome')
        }
      })
    },
    async loadAsyncRoutes() {
      let userInfo = storage.getItem('userInfo') || {}
      if (userInfo.token) {
        try {
          const menuList = await this.$api.getMenuList()
          let routes = generateRoute(menuList)
          routes.map(route => {
            let url = route.component
            for (const path in modules) {
              if (path.indexOf(url) !== -1) {
                route.component = modules[path]
                this.$router.addRoute('home', route)
              }
            }
          })
        } catch (error) {
          console.error(error)
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.login-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  background: url('../assets/images/login-bg.jpg') no-repeat;
  width: 100vw;
  height: 100vh;
  background-size: 100% 100%;
  position: relative;
  display: flex;
  justify-content: space-between;
  .info {
    display: flex;
    flex-direction: column;
    margin-left: 10%;
    .logo {
      background: url('../assets/images/login-logo.png') no-repeat;
      width: 180px;
      height: 150px;
      background-size: 100% 100%;
    }
    .text {
      background: url('../assets/images/login-text.png') no-repeat;
      width: 476px;
      height: 203px;
      background-size: 100% 100%;
      margin-top: 60px;
    }
  }
  .modal {
    width: 500px;
    padding: 30px;
    background-color: #fff;
    border-radius: 8px;
    box-shadow: 0px 0px 10px 3px #c7c9cb4d;
    margin-right: 10%;
    .title {
      font-size: 30px;
      line-height: 1.5;
      text-align: center;
      margin-bottom: 30px;
    }
    .btn-login {
      width: 100%;
    }
  }
}
</style>